const jwt = require('jsonwebtoken')
const { whiteRouter } = require('../config/app')
const { verify } = require('../utils/jwt')

module.exports = (req, res, next) => {
  const roures = whiteRouter.find(item => item === req.path)
  if (roures) return next()
  const authorization = req.headers.authorization && req.headers.authorization.split(' ').pop()
  if (!authorization) return res.error('非法登录!', 401, null)
  verify(authorization, (err, decode) => {
    if (err) return res.error('签名验证失败', 401, null)
    next()
  })
}